package com.wyp168.leetcode;

import java.util.Arrays;

/**
 * @ClassName PlusOneSolution
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2022/10/27 20:04
 */
public class PlusOneSolution {
    public static void main(String[] args) {
        int[] digits = {8, 9, 9, 9};
        PlusOneSolution solution = new PlusOneSolution();
        Arrays.stream(solution.plusOne(digits)).forEach(System.out::print);
    }

    public int[] plusOne(int[] digits) {
        int index = digits.length - 1;
        int up = 0;
        while (index >= 0) {
            if (index == digits.length - 1) {
                up = (digits[index] + 1) >= 10 ? 1 : 0;
                digits[index] = (digits[index] + 1) % 10;
                index --;
                continue;
            }
            int temp = digits[index];
            digits[index] = (digits[index] + up) % 10;
            up = (temp + up) >= 10 ? 1 : 0;
            index --;
        }

        if (up == 1) {  //数组扩容
            int[] result = new int[digits.length + 1];
            result[0] = 1;
            for(int i = 1; i < result.length; i ++) {
                result[i] = digits[i - 1];
            }
            return result;
        }
        return digits;
    }
}
